var e0_form = {};

e0_form._encode = function(payload, fieldName, fieldValue) {
    payload += fieldName + "=" + encodeURIComponent(fieldValue) + "&";
    return payload;
};

e0_form.serializeForm = function(form) {
	    if (typeof (form) == "string") {
		    var formObject = document.forms[form];
		    if (formObject == null)
			    formObject = document.getElementById(form);

		    form = formObject;
	    }

	    var formValues = "";
	    for ( var i = 0; i < form.elements.length; i++) {
		    var currentField = form.elements[i];
		    var fieldName = currentField.name;
		    var fieldType = currentField.type;

		    /*
			 * Disabled and unnamed fields are not sent by browsers so ignore
			 * them
			 */
		    if ((!currentField.disabled) && fieldName) {
			    switch (fieldType) {
			    case "text":
			    case "password":
			    case "hidden":
			    case "textarea":
				    formValues = e0_form._encode(formValues, fieldName, currentField.value);
				    break;
			    case "radio":
			    case "checkbox":
				    if (currentField.checked)
					    formValues = e0_form._encode(formValues, fieldName, currentField.value);
				    break;
			    case 'select-one':
			    case 'select-multiple':
				    for ( var j = 0; j < currentField.options.length; j++) {
					    if (currentField.options[j].selected) {
						    formValues = e0_form._encode(formValues, fieldName,
						            currentField.options[j].value);
					    }
				    }
				    break;
			    default:
				    continue; /* everything else like fieldset you don't want */
			    }
		    }

	    }
	    // pull of last &
	    if (formValues.length > 0) {
		    formValues = formValues.substring(0, formValues.length - 1);
	    }
	    return formValues;
};
e0_form.switchContent = function(containerId, url) {	
	switchContentWithEffect(containerId, url, true);
	return false;
}
e0_form.switchContentWithEffect = function(containerId, url, effect) {
	//$("#"+containerId).hide("slide", { direction: "up" }, 1000, function() {
	if (effect) {
		$("#"+containerId).slideToggle("fast", function () {
			$.ajax({
		        url: url, cache: false, dataType: 'html', type:"GET", async: true,
		        success: function (html) {	            	
		            $("#"+containerId).html(html);
		            $("#"+containerId).slideToggle("fast");
		            return false;
		        }
		    });
		});
	} else {
		$.ajax({
		    url: url, cache: false, dataType: 'html', type:"GET", async: true,
		    success: function (html) {	            	
		        $("#"+containerId).html(html);
		        return false;
		    }
		});
	}
	return false;
}
e0_form.openDialog = function(dialogId, title, getUrl, postUrl, formId, backUrl, containerId) {	
	$("#"+dialogId).dialog({
        autoOpen: false,
        modal: true,
        title: title,
        width: 580,
        open: function() {
        	$(this).html("Retrieving content...");
        	$(this).load(getUrl); 
        },
        buttons: {
            "Save": function() {
          	    var formData = e0_form.serializeForm(formId);
                if (containerId!='') {
          	      $("#"+containerId).html("");
                }
	            $.ajax({
	                url: postUrl, cache: false, dataType: "html", type:"POST", async: false,
	                data: formData, 
	                success: function (html) {
		                if (html.substring(0, 2) == "ok") { 
		                    $("#"+dialogId).dialog("close"); 
		                    $("#"+dialogId).remove();
		                    if (backUrl!='') {
			                    if (containerId=='') {
			                    	location.href = backUrl;
			                    } else {
			                    	switchContent(containerId, backUrl);
			                    }	
		                    }	                    
		                    return false;
		                } else {
		                	$("#"+dialogId).html(html);
		                    return false;
		                }    
	                }
	            });
            },
            "Cancel": function() { 
                $(this).dialog("close"); 
                $("#"+dialogId).empty();
            }
        }
    });  
	$("#"+dialogId).dialog("open");
	return false;
}